package com.gdpu.watermelontimer.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gdpu.watermelontimer.R
import com.gdpu.watermelontimer.data.Task
import com.gdpu.watermelontimer.viewmodel.MainViewModel
//RecyclerView 适配器，用于在主界面任务列表中展示每个任务卡片，并处理任务的相关操作

class TaskAdapter(
    private val viewModel: MainViewModel,
    private val onTaskStart: (Task) -> Unit,
    private val onTaskComplete: (Task) -> Unit,
    private val onTaskDelete: (Task) -> Unit
) : RecyclerView.Adapter<TaskAdapter.TaskViewHolder>() {

    private var tasks: List<Task> = emptyList()//保存当前要显示的任务列表

    fun updateTasks(newTasks: List<Task>) {
        tasks = newTasks.sortedWith(
            compareBy<Task> { it.isCompleted }//先看完成
                .thenBy { priorityOrder(it.priority) }//优先级第二
                .thenBy { it.createdAt }//最后看时间
        )
        notifyDataSetChanged()
    }

    private fun priorityOrder(priority: Task.Priority): Int {
        return when (priority) {
            Task.Priority.URGENT -> 0
            Task.Priority.IMPORTANT -> 1
            Task.Priority.ROUTINE -> 2
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_task, parent, false)
        return TaskViewHolder(view)
    }

    override fun onBindViewHolder(holder: TaskViewHolder, position: Int) {
        holder.bind(tasks[position])
    }

    override fun getItemCount(): Int = tasks.size

    inner class TaskViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val taskNameText: TextView = itemView.findViewById(R.id.task_name_text)
        private val taskDescriptionText: TextView = itemView.findViewById(R.id.task_description_text)
        private val priorityText: TextView = itemView.findViewById(R.id.priority_text)
        private val categoryText: TextView = itemView.findViewById(R.id.category_text)
        private val pomodoroCountText: TextView = itemView.findViewById(R.id.pomodoro_count_text)
        private val startTaskButton: Button = itemView.findViewById(R.id.start_task_button)
        private val completeTaskButton: Button = itemView.findViewById(R.id.complete_task_button)

        fun bind(task: Task) {
            taskNameText.text = task.name
            taskDescriptionText.text = task.description.ifEmpty { "无描述" }
            categoryText.text = task.category
            pomodoroCountText.text = "🍉 ${task.completedPomodoros}/${task.estimatedPomodoros}"
            // 优先级显示
            priorityText.text = when (task.priority) {
                Task.Priority.ROUTINE -> "日常"
                Task.Priority.IMPORTANT -> "重要"
                Task.Priority.URGENT -> "急"
            }
            priorityText.setTextColor(
                when (task.priority) {
                    Task.Priority.ROUTINE -> android.graphics.Color.parseColor("#8BC34A") // 绿色
                    Task.Priority.IMPORTANT -> android.graphics.Color.parseColor("#FF9800") // 橙色
                    Task.Priority.URGENT -> android.graphics.Color.parseColor("#F44336") // 红色
                }
            )
            if (task.isCompleted) {//变灰
                itemView.alpha = 0.5f
                startTaskButton.visibility = View.GONE
                completeTaskButton.text = "删除"
                completeTaskButton.visibility = View.VISIBLE
                completeTaskButton.setOnClickListener {
                    onTaskDelete(task)
                }
            } else {
                itemView.alpha = 1.0f
                startTaskButton.visibility = View.VISIBLE
                completeTaskButton.text = "完成"
                completeTaskButton.visibility = View.VISIBLE
                startTaskButton.setOnClickListener {
                    onTaskStart(task)
                }
                completeTaskButton.setOnClickListener {
                    onTaskComplete(task)
                }
            }
        }
    }
} 